#include "init.h"

cr_state *init() {
    cr_state *state = malloc(sizeof(cr_state));
    /* bool */
    state->render_change = false;
    state->incomplete_parse = false;
    state->file_changed = false;
    /* int */
    state->frame = 0;
    /* uint64_t */
    state->previous_info_hash = 0;
    /* button_node */
    state->root = NULL;
    state->tail = NULL;
    /* track */
    state->cr_track = NULL;
    /* player */
    state->cr_player = NULL;

    /* try to init SDL_ttf */
    if (TTF_Init() == -1) {
        fprintf(stderr, "TTF_Init error. %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    /* init rest of components */
    init_input();
    init_graphics(&state->window, &state->renderer, &state->display, &state->font, &state->render_change, &state->root, &state->tail);
    init_crg(&state->font, &state->render_change, &state->cr_track, &state->cr_player, &state->file_changed, &state->frame);

    return state;
}

/* post game cleanup */
void cleanup() {
    cleanup_crg(state->cr_track, state->cr_player);
    cleanup_graphics(&state->window, &state->renderer);
    cleanup_input(state->root);
    TTF_Quit();
    SDL_Quit();

    /* at last */
    free(state);
}